<template>
  <el-dialog
    class="main_reset_pwd_dialog"
    width="1000"
    :modelValue="modelValue"
    draggable
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    align-center
  >
    <div class="flex h-[460px] items-center">
      <div
        class="w-[380px] bg-[var(--base-pwd-bg)] h-full flex items-center justify-center"
      >
        <div>
          <img src="./imgs/pwd.png" alt="" class="block mx-auto" />
          <div class="text-center font-bold mb-[6px] text-[var(--base-text-color)]">
            系统账号安全管控
          </div>
          <div class="text-center text-[red]">
            使用过账号密码登录的用户,需每3个月定期修改密码
          </div>
        </div>
      </div>
      <div class="w-[620px] px-[64px]">
        <el-form ref="pwdRef" :model="user" :rules="rules" label-position="top">
          <el-form-item label="旧密码" prop="old_password">
            <el-input
              v-model="user.old_password"
              placeholder="请输入旧密码"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="新密码" prop="new_password">
            <el-input
              v-model="user.new_password"
              placeholder="请输入新密码"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="sure_password">
            <el-input
              v-model="user.sure_password"
              placeholder="请确认新密码"
              type="password"
              show-password
            />
            <div class="text-[red]" v-if="!isClose">
              提示:{{ pwd_overdue_tip }}
            </div>
          </el-form-item>
        </el-form>
        <div class="text-center">
          <el-button round class="mr-[30px]" :disabled="loading" @click="handleClose" v-if="isClose">关闭</el-button>
          <el-button type="primary" round v-if="!disabled" :loading="loading" @click="submit">确定</el-button>
        </div>
      </div>
    </div>

    <!-- <template #footer>
      <div class="flex items-center justify-end py-[10px]">
        <el-button :disabled="loading" @click="handleClose" v-if="isClose"
          >取消</el-button
        >
        <el-button
          v-if="!disabled"
          type="primary"
          :loading="loading"
          @click="submit"
        >
          保存
        </el-button>
      </div>
    </template> -->
  </el-dialog>
</template>

<script setup>
import { updateUserPwd } from "@/api/system/user";
import useUserStore from "@/store/modules/user";

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  isClose: { type: Boolean, default: true },
});
const loading = ref(false);
const userStore = useUserStore();
const pwd_overdue_tip = computed(() => userStore.pwd_overdue_tip);
const { proxy } = getCurrentInstance();

const emit = defineEmits(["update:modelValue"]);

const user = reactive({
  old_password: undefined,
  new_password: undefined,
  sure_password: undefined,
});

const equalToPassword = (rule, value, callback) => {
  if (user.new_password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};
const rules = ref({
  old_password: [
    { required: true, message: "旧密码不能为空", trigger: "blur" },
  ],
  new_password: [
    { required: true, message: "新密码不能为空", trigger: "blur" },
    { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  ],
  sure_password: [
    { required: true, message: "确认密码不能为空", trigger: "blur" },
    { required: true, validator: equalToPassword, trigger: "blur" },
  ],
});

/** 提交按钮 */
function submit() {
  proxy.$refs.pwdRef.validate((valid) => {
    if (valid) {
      const params = {
        ...user,
        user_id: userStore?.user_id,
      };
      updateUserPwd(params).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        handleClose();
      });
    }
  });
}

const handleClose = () => {
  emit("update:modelValue", false);
  proxy.$refs.pwdRef?.resetFields();
};
</script>
<style lang="scss">
.main_reset_pwd_dialog {
  .el-dialog__header {
    display: none;
  }
  .el-dialog__body {
    padding: 0 !important;
    border-radius: 12px;
  }
}
</style>
